﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using QLPM.DTO;

namespace QLPM.DAO
{
    public class ThuocDAO
    {
        DataProvider provider;

        public ThuocDAO()
        {
            provider = new DataProvider();
        }

        public List<Thuoc> DanhSachThuoc()
        {
            String sql = "select * from THUOC";
            provider = new DataProvider("THUOC",sql);
            DataTable dt = provider;
            List<Thuoc> ds = new List<Thuoc>();

            for(int i = 0; i < dt.Rows.Count; i++)
            {
                Thuoc t = new Thuoc();
                t.MaThuoc = Convert.ToInt32(dt.Rows[i]["MaThuoc"].ToString());
                t.TenThuoc = dt.Rows[i]["TenThuoc"].ToString();
                t.SoLuongTon = Convert.ToInt32(dt.Rows[i]["SoLuongTon"].ToString());
                ds.Add(t);
            }
            return ds;
        }

        public int ThemThuoc(Thuoc t)
        {
            String sql = String.Format("insert into THUOC(TenThuoc, SoLuongTon) values('{0}', {1})", t.TenThuoc, t.SoLuongTon);
            return provider.Excute_Non_Query(sql);
        }

        public int XoaThuoc(Thuoc t)
        {
            String sql = String.Format("delete from THUOC where MaThuoc={0}", t.MaThuoc);
            return provider.Excute_Non_Query(sql);
        }

        public int SuaThuoc(Thuoc t)
        {
            String sql = String.Format("update THUOC set TenThuoc='{0}', SoLuongTon={1} where MaThuoc={2}", t.TenThuoc, t.SoLuongTon, t.MaThuoc);
            return provider.Excute_Non_Query(sql);
        }

        public int LayDonGiaTheoMaThuoc(int maThuoc, int maDonVi)
        {
            String sql = String.Format("select DONGIA.DonGia from THUOC, DONGIA, DONVI where THUOC.MaThuoc = DONGIA.MaThuoc and DONVI.MaDonVi = DONGIA.MaDonVi and THUOC.MaThuoc={0} and DONVI.MaDonVi={1}", maThuoc,maDonVi);
            provider = new DataProvider("THUOC, DONGIA, DONVI", sql);
            DataTable dt = provider;
            return Convert.ToInt32(dt.Rows[0]["DonGia"].ToString());
        }

        public List<DonVi> LayDonViChoThuoc(int maThuoc)
        {
            String sql = String.Format("select DONVI.MaDonVi,DONVI.TenDonVi from THUOC,DONGIA,DONVI where THUOC.MaThuoc = DONGIA.MaThuoc and DONVI.MaDonVi = DONGIA.MaDonVi and THUOC.MaThuoc={0}", maThuoc);
            provider = new DataProvider("THUOC,DONGIA,DONVI", sql);
            DataTable dt = provider;
            List<DonVi> ds = new List<DonVi>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DonVi dv = new DonVi();
                dv.MaDonVi = Convert.ToInt32(dt.Rows[i]["MaDonVi"].ToString());
                dv.TenDonVi = dt.Rows[i]["TenDonVi"].ToString();
                ds.Add(dv);
            }
            return ds;
        }

         public DataTable SuDungThuoc(int Thang, int Nam)
        {
            string sql = String.Format("SELECT THUOC.TenThuoc, DONVI.TenDonVi, SUM(CHITIETKHAMBENH.SoLuong) AS SoLuongDung, COUNT(CHITIETKHAMBENH.SoLuong) AS SoLanDung FROM CHITIETKHAMBENH, THUOC, DONVI, KHAMBENH WHERE (((THUOC.MaThuoc)=CHITIETKHAMBENH.MaThuoc) And ((DONVI.MaDonVi)=CHITIETKHAMBENH.MaDonVi) And ((KHAMBENH.MaKhamBenh)=CHITIETKHAMBENH.MaKhamBenh) And ((Month(KHAMBENH.NgayKham))={0}) And ((Year(KHAMBENH.NgayKham))={1})) GROUP BY THUOC.TenThuoc, DONVI.TenDonVi", Thang, Nam);
            provider = new DataProvider("KHAMBENH,CHITIETKHAMBENH,THUOC,DONVI", sql);
            DataTable dt = provider;
            return dt;
        }
    }
}
